﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Text;

namespace SimpleIpc
{
    public class RemoteChannelFactory
    {
        private static ConcurrentDictionary<string, IRemoteChannel> channels;

        private RemoteChannelFactory()
        {

        }
        static RemoteChannelFactory()
        {
            channels = new ConcurrentDictionary<string, IRemoteChannel>();
        }

        public static IList<IRemoteChannel> GetChannels()
        {
            return channels.Select(item => item.Value).ToList();
        }

        public static IRemoteChannel CreateIpcChannel(string name)
        {
            return new IpcRemoteChannel(name);
        }

        public static IRemoteChannel CreateIpcChannel(string name, string portName)
        {
            return new IpcRemoteChannel(name, portName);
        }

        public static IRemoteChannel CreateTcpChannel(string name, int port = 0)
        {
            return new TcpRemoteChannel(name, port);
        }

        public static IRemoteChannel GetChannel(string name)
        {
            if (channels.ContainsKey(name))
                return channels[name] as IRemoteChannel;
            return null;
        }

        public static void AddChannel(IRemoteChannel channel)
        {
            if (channel != null)
                channels[channel.Name] = channel;
        }

        public static void RemoveChannel(string name)
        {
            if (channels.ContainsKey(name))
                channels.TryRemove(name, out IRemoteChannel channel);
        }
        public static void RemoveChannel(IRemoteChannel channel)
        {
            if (channel != null)
                RemoveChannel(channel.Name);
        }
    }
}
